<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside width="250px" class="layout-aside">
      <div class="aside-header">
        <h2 class="app-title">📚 知识库</h2>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        router
        class="aside-menu"
      >
        <el-menu-item index="/">
          <el-icon><Document /></el-icon>
          <span>所有笔记</span>
        </el-menu-item>
        <el-menu-item index="/notebooks">
          <el-icon><Folder /></el-icon>
          <span>笔记本</span>
        </el-menu-item>
        <el-menu-item index="/tags">
          <el-icon><CollectionTag /></el-icon>
          <span>标签</span>
        </el-menu-item>
      </el-menu>
      
      <div class="aside-footer">
        <el-button type="primary" class="new-note-btn" @click="createNewNote">
          <el-icon><Plus /></el-icon>
          新建笔记
        </el-button>
      </div>
    </el-aside>
    
    <!-- 主内容区 -->
    <el-container>
      <!-- 顶部栏 -->
      <el-header class="layout-header">
        <div class="header-left">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索笔记..."
            class="search-input"
            clearable
            @keyup.enter="handleSearch"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
        
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              <el-icon><User /></el-icon>
              <span>{{ userStore.user?.username || '用户' }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 内容区 -->
      <el-main class="layout-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useNoteStore } from '@/stores/note'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const noteStore = useNoteStore()

const searchKeyword = ref('')

const activeMenu = computed(() => route.path)

function createNewNote() {
  router.push('/editor')
}

function handleSearch() {
  if (searchKeyword.value.trim()) {
    noteStore.search(searchKeyword.value)
    if (route.path !== '/') {
      router.push('/')
    }
  }
}

function handleCommand(command) {
  if (command === 'logout') {
    userStore.logout()
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.layout-aside {
  background: #fff;
  border-right: 1px solid #e4e7ed;
  display: flex;
  flex-direction: column;
}

.aside-header {
  padding: 20px;
  border-bottom: 1px solid #e4e7ed;
}

.app-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.aside-menu {
  flex: 1;
  border-right: none;
}

.aside-footer {
  padding: 20px;
  border-top: 1px solid #e4e7ed;
}

.new-note-btn {
  width: 100%;
}

.layout-header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
}

.header-left {
  flex: 1;
  max-width: 400px;
}

.search-input {
  width: 100%;
}

.header-right {
  margin-left: 20px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background 0.3s;
}

.user-dropdown:hover {
  background: #f5f7fa;
}

.layout-main {
  background: #f5f7fa;
  padding: 24px;
  overflow-y: auto;
}
</style>

